/**
 * Copyright 2008 Matthew Purland (m.purland@gmail.com)
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.variance.version.identifier;

import java.util.Date;

/**
 * A date revision identifer that associates the revision of an item by a
 * date.  This would be used to associate a revision with a specific date
 * in time with millisecond precision.
 *
 * @author Matthew Purland
 */
public class DateRevisionIdentifier extends AbstractRevisionIdentifier<Date> {

    private Date dateIdentifier = null;

    public DateRevisionIdentifier(
            RevisionIdentifierSource revisionIdentifierSource,
            Date dateIdentifier) {
        super(revisionIdentifierSource);
        this.dateIdentifier = dateIdentifier;
    }

    /**
     * {@inheritDoc}
     */
    public int compareTo(RevisionIdentifier o) {
        if (o instanceof DateRevisionIdentifier) {
            DateRevisionIdentifier dateRevisionIdentifier = (DateRevisionIdentifier) o;

            // If the revision identifier is after
            if (getIdentifier().after(dateRevisionIdentifier.getIdentifier())) {
                return 1;
            }
            // If the revision identifier is before
            else if (dateRevisionIdentifier.getIdentifier().after(getIdentifier())) {
                return -1;
            }
            // If the revision identifier is equal
            else {

                return 0;
            }
        }
        else {
            throw new ClassCastException("Object cannot be compared with " + this.getClass().getName());
        }
    }

    /**
     * Get the internal revision identifer.
     */
    public Date getIdentifier() {
        return dateIdentifier;
    }
}
